package com.hyyt.shopjsp.callcenter.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.store.info.pojo.ShopShopInfo;
import com.hyyt.shopjsp.store.info.service.IShopShopInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.callcenter.pojo.ShopCustomerService;
import com.hyyt.shopjsp.callcenter.service.IShopCustomerServiceService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/04/26 0024上午 11:19.
 */

@Controller
@RequestMapping("/back/customerService")
public class ShopCustomerServiceController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopCustomerServiceController.class);
	private String basePath = "/back/shop/customerService/";
	@Resource
	private IShopCustomerServiceService shopCustomerServiceService;//客服service
	@Resource
	private IShopShopInfoService shopShopInfoService;//店铺信息service
	/**
	* 跳转至客服列表页面
	* @return 返回到指定页面
	*/
	@RequestMapping("gotoCustomerServicePage")
	public String gotoCustomerServicePage(){
		return basePath+"customerServiceList";
	}

	/**
	* 查找客服列表
	* @param shopName 店铺名称
	* @param trueName 真实姓名
	* @param nikeName 昵称
	* @param qq qq
	* @param useState 使用状态
	* @param page 分页
	* @param response 返回结果集
	*/
	@RequestMapping(value="listCustomerService",method=RequestMethod.POST)
	@ResponseBody
	public void listCustomerService(String shopInfoId,String shopName,String trueName,String nikeName,String qq,String useState,Page page, HttpServletResponse response){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopInfoId)){
			params.put("shopInfoId", shopInfoId);
		}
		if(Utils.objectIsNotEmpty(shopName)){
			params.put("shopName", shopName.trim());
		}
		if(Utils.objectIsNotEmpty(trueName)){
			params.put("trueName", trueName.trim());
		}
		if(Utils.objectIsNotEmpty(nikeName)){
			params.put("nikeName", nikeName.trim());
		}
		if(Utils.objectIsNotEmpty(qq)){
			params.put("qq", qq.trim());
		}
		if(Utils.objectIsNotEmpty(useState)){
			params.put("useState", useState);
		}
		int totalRecordCount =shopCustomerServiceService.countShopCustomerService(params);
		params.put("sortColumns", "customerServiceId desc");
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<Map<String, Object>> list =shopCustomerServiceService.queryShopCustomerService(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	* 保存或者修改客服
	*
	* @param shopCustomerService 客服对象
	* @return  返回数据
	*/
	@RequestMapping(value = "saveOrUpdateCustomerService", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateCustomerService(ShopCustomerService shopCustomerService) {
		boolean flag=false;
		String flagg="";
		if (Utils.objectIsNotEmpty(shopCustomerService)) {
			if (Utils.objectIsNotEmpty(shopCustomerService.getCustomerServiceId())) {
				shopCustomerServiceService.updateShopCustomerService(shopCustomerService);
				flag=true;
				flagg="false";
			} else {
				shopCustomerServiceService.saveShopCustomerService(shopCustomerService);
				flag=true;
				flagg="true";
			}
			return ResultMsg.create(flag,flagg);
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	* 删除传入的客服id
	*
	* @param customerServiceId  客服id
	* @return  boolean  返回布尔值
	*/
	@RequestMapping(value = "deleteCustomerServices", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteCustomerServices(String customerServiceId) {
		if (Utils.objectIsNotEmpty(customerServiceId)) {
			shopCustomerServiceService.deleteShopCustomerService(Integer.parseInt(customerServiceId));
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	* 根据客服id获得客服集合
	*
	* @param customerServiceId  客服id
	*/
	@RequestMapping(value = "getCustomerServiceInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg getCustomerServiceInfo(Integer customerServiceId) {
		if (Utils.objectIsNotEmpty(customerServiceId)) {
			Map<String,Object> param = new HashMap<>();
			param.put("customerServiceId",customerServiceId);
			List<Map<String,Object>> list  = shopCustomerServiceService.queryShopCustomerServiceDetail(param);//查询店铺客服信息
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(list));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 通过店铺客服查询所有店铺分类
	 * @param shopName 店铺名称
	 * @param page 分页
	 * @param response 返回结果集
	 */
	@RequestMapping(value="listShopInfo",method=RequestMethod.POST)
	@ResponseBody
	public void listShopInfo(String shopName,Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopName)){
			param.put("shopName", shopName.trim());
		}
		param.put("sortColumns", "shopInfoId asc");
		int totalRecordCount =shopShopInfoService.countForShopCustomerService(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShopInfo> shopInfoListMap =shopShopInfoService.queryShopShopInfoForShopCustomerService(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", shopInfoListMap);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 进入店铺客服管理页
	 * @params purviewId
	 * @params params
	 * @return
	 */
	@RequestMapping("gotoInStoreCustomerService")
	public String gotoInStoreCustomerService(String shopInfoId,Map<String, Object> param){
		param.put("shopInfoId", shopInfoId);
		return "/back/shop/inStoreCustomerService/inStoreCustomerServiceList";
	}

}
